﻿using Microsoft.Extensions.DependencyInjection;
using SimpleORM.Extensions;
using SimpleORM.Model.SYSModel;
using SqlSugar.IOC;

namespace SimpleORM.Infrastructure
{
    public static class SqlsugarSetup
    {
        public static void AddSqlsugarSetup(this IServiceCollection services)
        {
            var conns = new List<ConnectStr>();
            AppSettings.Bind<List<ConnectStr>>("ConnectionStrings", conns);
            ArgumentNullException.ThrowIfNull(nameof(services));
            ArgumentNullException.ThrowIfNull(nameof(conns));

            var iocs = conns
                .Select(a => new IocConfig
                {
                    ConfigId = a.ConfigId,// 多租户用到,单库则默认0
                    ConnectionString = a.Conn_db,
                    DbType = a.Conn_db_type,
                    IsAutoCloseConnection = true,//自动释放
                }).ToList();
            services.AddSqlSugar(iocs);
        }
    }

}
